# 自定义中间类（执行是否登录判断）
from django.shortcuts import redirect
from django.urls import reverse
import re


class ShopMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.
        print("ShopMiddleware")

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        path = request.path
        print("url", path)
        # 判断管理后台是否登录
        # 定义后台不登录也可以直接访问的rul列表
        urllist = ['/myadmin/login', '/myadmin/logout', '/myadmin/dologin', '/myadmin/verify']
        # 判断当前请求url地址是否以/myadmin，并且不在urllist中，才做是否登录判断
        if re.match(r'^/myadmin', path) and (path not in urllist):
            if 'adminuser' not in request.session:
                # 重定向到登入页
                return redirect(reverse("myadmin_login"))

        # 判断管理前台是否登录
        # 判断当前请求url地址是否以/web，才做是否登录判断
        if re.match(r'^/web', path):
            if 'webuser' not in request.session:
                # 重定向到登入页
                return redirect(reverse("web_login"))

        # 判断移动端是否登录
        # 定义移动端不登录也可以直接访问的rul列表
        urllist = ['/mobile/register', '/mobile/doregister']
        # 判断当前请求url地址是否以/mobile，并且不在urllist中，才做是否登录判断
        if re.match(r'^/mobile', path) and (path not in urllist):
            if 'mobileuser' not in request.session:
                # 重定向到登入页
                return redirect(reverse("mobile_register"))
        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response
